define([], function () {
    require.config({
    paths: {
        'simditor': '../addons/simditor/js/simditor.min',
    },
    shim: {
        'simditor': [
            'css!../addons/simditor/css/simditor.min.css',
        ]
    }
});
require(['form'], function (Form) {
    var _bindevent = Form.events.bindevent;
    Form.events.bindevent = function (form) {
        _bindevent.apply(this, [form]);
        if ($(Config.simditor.classname || '.editor', form).length > 0) {
            //修改上传的接口调用
            require(['upload', 'simditor'], function (Upload, Simditor) {
                var editor, mobileToolbar, toolbar;
                Simditor.locale = 'zh-CN';
                Simditor.list = {};
                toolbar = ['title', 'bold', 'italic', 'underline', 'strikethrough', 'fontScale', 'color', '|', 'ol', 'ul', 'blockquote', 'code', 'table', '|', 'link', 'image', 'hr', '|', 'indent', 'outdent', 'alignment'];
                mobileToolbar = ["bold", "underline", "strikethrough", "color", "ul", "ol"];
                $(Config.simditor.classname || '.editor', form).each(function () {
                    var id = $(this).attr("id");
                    editor = new Simditor({
                        textarea: this,
                        height: isNaN(Config.simditor.height) ? null : parseInt(Config.simditor.height),
                        minHeight: parseInt(Config.simditor.minHeight || 250),
                        toolbar: Config.simditor.toolbar || [],
                        mobileToolbar: Config.simditor.mobileToolbar || [],
                        toolbarFloat: parseInt(Config.simditor.toolbarFloat),
                        placeholder: Config.simditor.placeholder || '',
                        pasteImage: true,
                        defaultImage: Config.__CDN__ + '/assets/addons/simditor/images/image.png',
                        upload: {url: '/'},
                        allowedTags: ['div', 'br', 'span', 'a', 'img', 'b', 'strong', 'i', 'strike', 'u', 'font', 'p', 'ul', 'ol', 'li', 'blockquote', 'pre', 'code', 'h1', 'h2', 'h3', 'h4', 'hr'],
                        allowedAttributes: {
                            div: ['data-tpl', 'data-source', 'data-id'],
                            span: ['data-id']
                        },
                        allowedStyles: {
                            div: ['width', 'height', 'padding', 'background', 'color', 'display', 'justify-content', 'border', 'box-sizing', 'max-width', 'min-width', 'position', 'margin-left', 'bottom', 'left', 'margin', 'float'],
                            p: ['margin', 'color', 'height', 'line-height', 'position', 'width', 'border', 'bottom', 'float'],
                            span: ['text-decoration', 'color', 'margin-left', 'float', 'background', 'padding', 'margin-right', 'border-radius', 'font-size', 'border', 'float'],
                            img: ['vertical-align', 'width', 'height', 'object-fit', 'float', 'margin', 'float'],
                            a: ['text-decoration']
                        }
                    });
                    editor.uploader.on('beforeupload', function (e, file) {
                        Upload.api.send(file.obj, function (data) {
                            var url = Fast.api.cdnurl(data.url);
                            editor.uploader.trigger("uploadsuccess", [file, {success: true, file_path: url}]);
                        });
                        return false;
                    });
                    editor.on("blur", function () {
                        this.textarea.trigger("blur");
                    });
                    if (editor.opts.height) {
                        editor.body.css({height: editor.opts.height, 'overflow-y': 'auto'});
                    }
                    if (editor.opts.minHeight) {
                        editor.body.css({'min-height': editor.opts.minHeight});
                    }
                    Simditor.list[id] = editor;
                });
            });
        }
    }
});



require.config({
    paths: {
        'leaflet': '../addons/wleaflet/leaflet/leaflet',
        'proj4':'/assets/addons/wleaflet/leaflet/plugins/proj4/proj4',
        'proj4leaflet_min':'/assets/addons/wleaflet/leaflet/plugins/proj4/proj4leaflet.min',
        'leaflet_ChineseTmsProviders':'/assets/addons/wleaflet/leaflet/leaflet.ChineseTmsProviders',
        'mapCorrection':'/assets/addons/wleaflet/leaflet/leaflet.mapCorrection',
        'leaflet_geoman_min':'/assets/addons/wleaflet/leaflet/plugins/leaflet-geoman-free/leaflet-geoman.min',
        'leaflet_common': '../addons/wleaflet/leaflet/leaflet_common'
    },
    shim: {
        'leaflet':{
            deps:[
                'css!../addons/wleaflet/leaflet/leaflet.css',
            ],
            exports:'L'
        },
        'leaflet_common': {
            deps:[
                'css!../addons/wleaflet/leaflet/plugins/leaflet-geoman-free/leaflet-geoman.css',
            ],
            exports: 'wLeaflet',
        },
    }
});

window.initWleafletFunc = function(callback)
{
    try{
        require(['proj4leaflet_min'], function () {
            require(['leaflet_ChineseTmsProviders'],function () {
                require(['mapCorrection','leaflet_geoman_min'],function () {
                    require(['leaflet_common'],function () {
                        if(typeof callback == 'function')
                        {
                            callback(wLeaflet,L)
                        }else{
                            console.log('缺少callback参数');
                        }
                    })
                })
            })
        })
    }catch (e) {
        if(typeof callback == 'function')
        {
            callback({})
        }else{
            console.log('初始化地图插件失败')
        }
    }
}

require([], function () {

    //绑定data-toggle=addresspicker属性点击事件
    $(document).on('click', "[data-toggle='wleaflet-select']", function () {
        var that = this;
        var callback = $(that).data('callback');
        var input_id = $(that).data("input-id") ? $(that).data("input-id") : "";
        var sel_type = $(that).data('type')? $(that).data("type") : "address"; //默认选择地理位置
        var url = "/index.php/addons/wleaflet/index/select?sel_type="+sel_type;
        switch (sel_type) {
            case 'address':
                //选择地理位置
                var lat_id = $(that).data("lat-id") ? $(that).data("lat-id") : "";
                var lng_id = $(that).data("lng-id") ? $(that).data("lng-id") : "";
                var address = input_id ? $("#" + input_id).val() : '';
                var lat = lat_id ? $("#" + lat_id).val() : '';
                var lng = lng_id ? $("#" + lng_id).val() : '';
                if(lat && lng)
                {
                    url = url +'&lat=' + lat + '&lng=' + lng;
                }
                if(address)
                {
                    url = url + '&address='+address;
                }
                break;
            case 'line':
                var limit_line_count = $(that).data("limit-line-count") ? $(that).data("limit-line-count") : 0;
                var latlng = input_id ? $("#" + input_id).val() : '';
                url = url +'&latlng='+latlng+'&limit_line_count='+limit_line_count;
                break;
            case 'marker':
            case 'polygon':
                //选择marker点
                var latlng = input_id ? encodeURIComponent( $("#" + input_id).val()) : '';
                url = url +'&latlng='+latlng;
                break;
            default:
                url = '';
                break;
        }
        if(url)
        {
            Fast.api.open(url, '地图操作', {
                callback: function (res) {

                    switch (sel_type) {
                        case 'address':
                            input_id && $("#" + input_id).val(res.address);
                            lat_id && $("#" + lat_id).val(res.lat);
                            lng_id && $("#" + lng_id).val(res.lng);
                            break;
                        case 'marker':
                        case 'line':
                        case 'polygon':
                            input_id && $("#" + input_id).val(res.latlng);
                            break;
                        default:
                            break;
                    }
                    try {
                        //执行回调函数
                        if (typeof callback === 'function') {
                            callback.call(that, res);
                        }
                    } catch (e) {

                    }
                }
            });
        }

    });
});
});